package org.rency.ues.domainservice.crypto.support;

import org.rency.common.utils.enums.CharsetEnum;
import org.rency.common.utils.security.DESUtils;
import org.rency.ues.common.enums.EncryptType;
import org.rency.ues.domainservice.crypto.AbstractCryptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DESCryptor extends AbstractCryptor {
	
	private static final Logger logger = LoggerFactory.getLogger(DESCryptor.class);

	public DESCryptor(EncryptType t) {
		super(t);
	}

	@Override
	public String encrypt(byte[] plainData, Object[] parameters) {
		try {
            if (parameters == null || parameters.length != 1) {
                logger.error("Invalid parameters.");
            }
            if (parameters[0] instanceof String) {
                String key = (String) parameters[0];
                return DESUtils.encryptDESede(new String(plainData, CharsetEnum.UTF_8.getCode()), key);
            } else {
                logger.error("Invalid parameters.");
            }
        } catch (Exception e) {
            logger.error("exception:", e);
        }
        return null;
	}

	@Override
	public byte[] decrypt(String enData, Object[] parameters) {
		try {
            if (parameters == null || parameters.length != 1) {
                logger.error("Invalid parameters.");
            }

            if (parameters[0] instanceof String) {
                String key = (String) parameters[0];
                String result = DESUtils.decryptDESede(enData, key);
                return result.getBytes(CharsetEnum.UTF_8.getCode());

            } else {
                logger.error("Invalid parameters.");
            }
        } catch (Exception e) {
            logger.error("exception:", e);
        }
        return null;
	}

}
